package sisicotren.ui.windows;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.util.Iterator;
import java.util.List;

import javax.swing.JDialog;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import sisicotren.model.simulator.SimulationStats;

public class SimulationResultGraphic extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private List<SimulationStats> datasource;
	private boolean variableMuLambdaReport = false;
	/**
	 * @param owner
	 */
	public SimulationResultGraphic(Frame owner, List<SimulationStats> stats) {
		super(owner);
		this.datasource = stats;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setContentPane(getJContentPane());
		this.pack();
		this.setLocationByPlatform(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			this.variableMuLambdaReport = isVariableMuLambda(this.datasource);
			DefaultCategoryDataset dataset = buildDataset();
			double lambda = 0.00;
			double mu = 0.00;
			if( (! this.datasource.isEmpty() ) && (! this.variableMuLambdaReport) ) {
				lambda = this.datasource.get(0).getLambdaUsed();
				mu = this.datasource.get(0).getMuUsed();
			}
			String YAxisText = this.variableMuLambdaReport ? "Valores" : "Lambda [" + lambda + "] - Mu [" + mu + "]";
			JFreeChart chart = ChartFactory.createBarChart3D("Arribos en Colas", "Corridas", YAxisText, dataset, PlotOrientation.VERTICAL, true, true, false);
			jContentPane = new ChartPanel(chart);
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}

	private DefaultCategoryDataset getVariableMuLambdaDataset() {
		
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		Iterator<SimulationStats> it = this.datasource.iterator();
		int corridaNum = 1;
		while(it.hasNext()) {
			SimulationStats currStat = it.next();
			dataset.setValue(currStat.getLambdaUsed(), "Corrida " + corridaNum, "Lambda");
			dataset.setValue(currStat.getMuUsed(), "Corrida " + corridaNum, "Mu");
			dataset.setValue(currStat.getTotalClients(), "Corrida " + corridaNum, "Clientes");
			// tamaños de cola.
			Iterator<Integer> itQueueSizes = currStat.getMaxQueueSizes().iterator();
			int colaNum = 1;
			while(itQueueSizes.hasNext()) {
				dataset.setValue(itQueueSizes.next(), "Corrida " + corridaNum, "Cola " + colaNum);
				colaNum++;
			}
			
			corridaNum++;
		}
		
		return dataset;
	}
	
private DefaultCategoryDataset getDefaultDataset() {
		
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		Iterator<SimulationStats> it = this.datasource.iterator();
		int corridaNum = 1;
		while(it.hasNext()) {
			SimulationStats currStat = it.next();
			dataset.setValue(currStat.getTotalClients(), "Corrida " + corridaNum, "Clientes");
			// tamaños de cola.
			Iterator<Integer> itQueueSizes = currStat.getMaxQueueSizes().iterator();
			int colaNum = 1;
			while(itQueueSizes.hasNext()) {
				dataset.setValue(itQueueSizes.next(), "Corrida " + corridaNum, "Cola " + colaNum);
				colaNum++;
			}
			
			corridaNum++;
		}
		
		return dataset;
	}
	
	private static Boolean isVariableMuLambda(List<SimulationStats> datasource) {
		boolean changed = false;
		// TODO : optimizar esto
		Iterator<SimulationStats> it = datasource.iterator();
		SimulationStats currStat = it.next();
		double mu = currStat.getMuUsed();
		double lambda = currStat.getLambdaUsed();
		while(it.hasNext() && !changed) {
			currStat = it.next();
			changed = (mu != currStat.getMuUsed()) || (lambda != currStat.getLambdaUsed());
		}
		
		return changed;
	}
	
	private DefaultCategoryDataset buildDataset() {
		DefaultCategoryDataset dataset = null;
		if(variableMuLambdaReport)
			dataset = this.getVariableMuLambdaDataset();
		else
			dataset = this.getDefaultDataset();
		return dataset;
	}
	
}
